/*
 * LdapDAO.java
 *
 * Created on 8 de marzo de 2007, 04:03 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.src.computec.radar.services.utilities.dao;
import com.src.computec.radar.base.dao.exception.DAOException;
import com.src.computec.radar.base.ldap.dao.GenericLdapDAO;
import com.src.computec.radar.base.ldap.dto.LdapUserDto;
import com.src.jndi.SRCLDAP;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.naming.directory.SearchControls;

/**
 *
 * @author aocampo
 */
public class LdapDAO extends GenericLdapDAO implements BaseLdapDAO {
    
    /** Creates a new instance of LdapDAO */
    public LdapDAO() {
    }
    
    public LdapUserDto getLdapUserByPrimaryKey(LdapUserDto dto) throws DAOException {
        try {
            String uid = dto.getIdLdap();
            SRCLDAP ldap = new SRCLDAP();
            ldap.connect(2, LDAP_DIR, LDAP_USER, LDAP_PASSWORD);
            HashMap resgr = ldap.search(SearchControls.SUBTREE_SCOPE, LDAP_PEOPLE_SID , "(uid="+uid+")", new String[] {LDAP_CODIGO_GRUPO_ATTRIBUTE_NAME,LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME,LDAP_CODIGO_EM_ATTRIBUTE_NAME,LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME,LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME,"cn","mail","telephonenumber","uid","entrydn","nsrole"});
            ldap.disconnect();
            Iterator iter = resgr.keySet().iterator();
            String hashId;
            for(;iter.hasNext();) {
                LdapUserDto retornoDto = new LdapUserDto();
                hashId = (String)iter.next();
                HashMap aux = (HashMap)resgr.get(hashId);
                if(aux.get(LDAP_CODIGO_GRUPO_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_CODIGO_GRUPO_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoGrupo(((ArrayList)aux.get(LDAP_CODIGO_GRUPO_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoCliente(((ArrayList)aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoEmpresaMensajeria(((ArrayList)aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoResponsable(((ArrayList)aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME)).isEmpty()){
                    retornoDto.setDirUsuarioAB(((ArrayList)aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get("cn") != null && !((ArrayList)aux.get("cn")).isEmpty()) {
                    retornoDto.setNombre(((ArrayList)aux.get("cn")).get(0).toString());
                }
                if(aux.get("mail") != null && !((ArrayList)aux.get("mail")).isEmpty()) {
                    retornoDto.setEmail(((ArrayList)aux.get("mail")).get(0).toString());
                }
                if(aux.get("telephonenumber") != null && !((ArrayList)aux.get("telephonenumber")).isEmpty()) {
                    retornoDto.setTelefono(((ArrayList)aux.get("telephonenumber")).get(0).toString());
                }
                if(aux.get("entrydn") != null && !((ArrayList)aux.get("entrydn")).isEmpty()) {
                    retornoDto.setDn(((ArrayList)aux.get("entrydn")).get(0).toString());
                }
                if(aux.get("uid") != null && !((ArrayList)aux.get("uid")).isEmpty()) {
                    retornoDto.setIdLdap(((ArrayList)aux.get("uid")).get(0).toString());
                }
                if(aux.get("nsrole") != null && !((ArrayList)aux.get("nsrole")).isEmpty()){
                    List rolesAux = (ArrayList)aux.get("nsrole");
                    List roles = new ArrayList();
                    for(int i=0; i < rolesAux.size(); i++) {
                        String role = (String)rolesAux.get(i);
                        String[] tokens = role.split(",");
                        String uidAux=tokens[0];
                        tokens = uidAux.split("=");
                        roles.add(tokens[1]);
                    }
                    retornoDto.setRoles(roles);
                }
                return retornoDto;
            }
        }
        catch (Exception e) {
            throw new DAOException("GenericLdapDAO:getLdapUserByPrimaryKey: " +e.toString(), e);
        }
        return null;
    }    
    
    public List getAllAuditores() throws DAOException {
        try {
            List auditores=new ArrayList();
            SRCLDAP ldap = new SRCLDAP();
            ldap.connect(2, LDAP_DIR, LDAP_USER, LDAP_PASSWORD);
            String roleDN = "cn=" + this.LDAP_ROL_AUDITOR_CN.toLowerCase() + "," + this.LDAP_ROOT;
            HashMap resgr = ldap.search(SearchControls.SUBTREE_SCOPE, LDAP_PEOPLE_SID , "(nsrole="+roleDN+")", new String[] {LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME,LDAP_CODIGO_EM_ATTRIBUTE_NAME,LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME,LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME,"cn","mail","telephonenumber","uid","entrydn","nsrole"});
            ldap.disconnect();
            Iterator iter = resgr.keySet().iterator();
            String hashId;
            for(;iter.hasNext();) {
                LdapUserDto retornoDto = new LdapUserDto();
                hashId = (String)iter.next();
                HashMap aux = (HashMap)resgr.get(hashId);
                if(((ArrayList)aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME)) != null && !((ArrayList)aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoCliente(((ArrayList)aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(((ArrayList)aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME)) != null && !((ArrayList)aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoEmpresaMensajeria(((ArrayList)aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(((ArrayList)aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME)) != null && !((ArrayList)aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoResponsable(((ArrayList)aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(((ArrayList)aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME)) != null && !((ArrayList)aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setDirUsuarioAB(((ArrayList)aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(((ArrayList)aux.get("cn")) != null && !((ArrayList)aux.get("cn")).isEmpty()) {
                    retornoDto.setNombre(((ArrayList)aux.get("cn")).get(0).toString());
                }
                if(((ArrayList)aux.get("mail")) != null && !((ArrayList)aux.get("mail")).isEmpty()) {
                    retornoDto.setEmail(((ArrayList)aux.get("mail")).get(0).toString());
                }
                if(((ArrayList)aux.get("telephonenumber")) != null && !((ArrayList)aux.get("telephonenumber")).isEmpty()) {
                    retornoDto.setTelefono(((ArrayList)aux.get("telephonenumber")).get(0).toString());
                }
                if(((ArrayList)aux.get("entrydn")) != null && !((ArrayList)aux.get("entrydn")).isEmpty()) {
                    retornoDto.setDn(((ArrayList)aux.get("entrydn")).get(0).toString());
                }
                if(((ArrayList)aux.get("uid")) != null && !((ArrayList)aux.get("uid")).isEmpty()) {
                    retornoDto.setIdLdap(((ArrayList)aux.get("uid")).get(0).toString());
                }
                if(((ArrayList)aux.get("nsrole")) != null && !((ArrayList)aux.get("nsrole")).isEmpty()) {
                    ArrayList rolesAux=(ArrayList)aux.get("nsrole");
                    ArrayList roles=new ArrayList();
                    for(int i=0;i<rolesAux.size();i++) {
                        String role=(String)rolesAux.get(i);
                        String[] tokens=role.split(",");
                        String uidAux=tokens[0];
                        tokens=uidAux.split("=");
                        roles.add(tokens[1]);
                    }
                    retornoDto.setRoles(roles);
                }
                auditores.add(retornoDto);
            }      
            return auditores;
        }
        catch (Exception e) {
            throw new DAOException("LdapDAO:getAllAuditores: " +e.toString(), e);
        }
    }
    
    public List getDestinatariosByCliente(String idCliente) throws DAOException {
        try {
            List destinatarios = new ArrayList();
            SRCLDAP ldap = new SRCLDAP();
            ldap.connect(2, LDAP_DIR, LDAP_USER, LDAP_PASSWORD);
            String roleDN = "cn=" + this.LDAP_ROL_CLIENTE_CN.toLowerCase() + "," + this.LDAP_ROOT;
            HashMap resgr = ldap.search(SearchControls.SUBTREE_SCOPE, LDAP_PEOPLE_SID , "(&(nsrole="+roleDN+")("+this.LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME+"="+idCliente+"))", new String[] {LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME,LDAP_CODIGO_EM_ATTRIBUTE_NAME,LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME,LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME,"cn","mail","telephonenumber","uid","entrydn","nsrole"});
            ldap.disconnect();
            Iterator iter = resgr.keySet().iterator();
            String hashId;
            for(;iter.hasNext();) {   
                LdapUserDto retornoDto = new LdapUserDto();
                hashId = (String)iter.next();
                HashMap aux = (HashMap)resgr.get(hashId);
                if(aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoCliente(((ArrayList)aux.get(LDAP_CODIGO_CLIENTE_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoEmpresaMensajeria(((ArrayList)aux.get(LDAP_CODIGO_EM_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setCodigoResponsable(((ArrayList)aux.get(LDAP_CODIGO_RESPONSABLE_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME) != null && !((ArrayList)aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME)).isEmpty()) {
                    retornoDto.setDirUsuarioAB(((ArrayList)aux.get(LDAP_DIR_USUARIO_AB_ATTRIBUTE_NAME)).get(0).toString());
                }
                if(aux.get("cn") != null && !((ArrayList)aux.get("cn")).isEmpty()) {
                    retornoDto.setNombre(((ArrayList)aux.get("cn")).get(0).toString());
                }
                if(aux.get("mail") != null && !((ArrayList)aux.get("mail")).isEmpty()) {
                    retornoDto.setEmail(((ArrayList)aux.get("mail")).get(0).toString());
                }
                if(aux.get("telephonenumber") != null && !((ArrayList)aux.get("telephonenumber")).isEmpty()) {
                    retornoDto.setTelefono(((ArrayList)aux.get("telephonenumber")).get(0).toString());
                }
                if(aux.get("entrydn") != null && !((ArrayList)aux.get("entrydn")).isEmpty()) {
                    retornoDto.setDn(((ArrayList)aux.get("entrydn")).get(0).toString());
                }
                if(aux.get("uid") != null && !((ArrayList)aux.get("uid")).isEmpty()) {
                    retornoDto.setIdLdap(((ArrayList)aux.get("uid")).get(0).toString());
                }
                if(aux.get("nsrole") != null && !((ArrayList)aux.get("nsrole")).isEmpty()) {
                    List rolesAux = (ArrayList)aux.get("nsrole");
                    List roles=new ArrayList();
                    for(int i=0;i<rolesAux.size();i++) {
                        String role=(String)rolesAux.get(i);
                        String[] tokens=role.split(",");
                        String uidAux=tokens[0];
                        tokens=uidAux.split("=");
                        roles.add(tokens[1]);
                    }
                    retornoDto.setRoles(roles);
                }
                destinatarios.add(retornoDto);
            }      
            return destinatarios;
        }
        catch (Exception e) {
            throw new DAOException("LdapDAO:getDestinatariosByCliente: " +e.toString(), e);
        } 
        finally {
            //closeConnection(con, statement);
        }     
    }
}